import time
from unittest import TestCase

import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestLogin(TestCase):
    def test_userlogin(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        title = chromeDriver.find_element(By.ID, "logo").text
        if title == '个人理财系统':
            print('成功')
            time.sleep(3)
            chromeDriver.save_screenshot('1.jpg')
        self.assertEqual(title, '个人理财系统')
        time.sleep(1)
        chromeDriver.close()

    def test_adminlogin(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('admin',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.find_element(By.ID, "username").send_keys("admin")
        chromeDriver.find_element(By.ID, "password").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        title = chromeDriver.find_element(By.ID, "logo").text
        if title == '后台管理系统':
            print('成功')
        else:
            print('失败')
        time.sleep(3)
        chromeDriver.save_screenshot('2.jpg')
        self.assertEqual(title, '后台管理系统')
        time.sleep(1)
        chromeDriver.close()

    def test_no_user(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertNotIn(('lisi3',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi3")
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        time.sleep(1)
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        t = chromeDriver.find_element(By.XPATH, "//html/body/div/div/div/form/div[1]/span[1]").text
        if t == '用户名不存在':
            print('成功')
        time.sleep(3)
        chromeDriver.save_screenshot('3.jpg')
        self.assertEqual(t, '用户名不存在')
        time.sleep(1)
        chromeDriver.close()

    def test_user_password(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get('http://localhost:90')
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys('lisi')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("1234567")
        time.sleep(1)
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        t = chromeDriver.find_element(By.XPATH, "//html/body/div/div/div/form/div[2]/span[1]").text
        if t == '密码错误':
            print('成功')
        time.sleep(3)
        chromeDriver.save_screenshot('4.jpg')
        self.assertEqual(t, '密码错误')
        time.sleep(1)
        chromeDriver.close()

    def test_no_admin(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertNotIn(('admin1',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin1")
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        time.sleep(1)
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        t = chromeDriver.find_element(By.XPATH, "//html/body/div/div/div/form/div[1]/span[1]").text
        if t == '用户名不存在':
            print('成功')
        time.sleep(3)
        chromeDriver.save_screenshot('5.jpg')
        self.assertEqual(t, '用户名不存在')
        time.sleep(1)
        chromeDriver.close()

    def test_admin_password(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('admin',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin")
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("1234567")
        time.sleep(1)
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        t = chromeDriver.find_element(By.XPATH, "//html/body/div/div/div/form/div[2]/span[1]").text
        if t == '密码错误':
            print('成功')
        time.sleep(3)
        chromeDriver.save_screenshot('6.jpg')
        self.assertEqual(t, '密码错误')
        time.sleep(1)
        chromeDriver.close()
